<?php
/**
 * 积分模块
 * @author Administrator
 *
 */
class integral extends CControllerMerchantBase{
	
	public function add()
	{
		$this->_title= "增加积分";
		
		if($_POST)
		{
			$this->searchOwner();
		}
		else{
			$this->setViewParam("search_type", "card");
			$this->setViewParam("s_value", "");
			$this->setViewParam("ownerExist", 0);
			
			$this->setJsBlock("$('#txt_card').focus()");
		}
		
		$this->render("integral/add");
	}
	
	public function reduce() {
		$this->_title= "扣减积分";
		if($_POST)
		{
			$this->searchOwner();
			
			$supplierModel = new supplierModel(); 
			$sInfo = $supplierModel->selectDataSetByWhere("","s_name,s_id",array(),"del_flag=0 and shop_id=".$this->_shopID);
			$this->setViewParam("sInfo", $sInfo);
		}
		else{
			$this->setViewParam("search_type", "card");
			$this->setViewParam("s_value", "");
			$this->setViewParam("ownerExist", 0);
				
			$this->setJsBlock("$('#txt_card').focus()");
		}
		
		$this->render("integral/reduce");
	}
	
	/**
	 * 根据卡号和手机号搜索业主
	 */
	private function searchOwner()
	{
		$ownerExist=0;
		$s_type = "card";
		$s_value = "";
		
		$s_type=$this->getRequestData("r_search_type");
		$s_value = "";
		$where = array();
		$ownerModel = new ownerModel();
		if($s_type=="phone")
		{
			$s_value = $this->getRequestData("txt_phone");
			$where["phone"] = array("v"=>$s_value,"table"=>"o");
		}
		else{
			$s_value = $this->getRequestData("txt_card");
			$where["supplementary_card"] = $s_value;
		}
			
		$ownerInfo = $ownerModel->selectByWhere("t_owner o,t_community c","o.*,c.community_name,c.province,c.city,c.area",$where, "o.community_id=c.community_id and o.del_flag=0");
	
		if($ownerInfo){
			$ownerExist = 1;
			$this->setViewParam("ownerInfo", $ownerInfo);
	
			$ownerID = $ownerInfo["owner_id"];
	
			$ownerShopRe = $ownerModel->selectByWhere(
			"t_owner_shop",
			"os_id,owner_id,shop_id,balance,owner_card_status,c_discount_type,c_discount",
			array("owner_id"=>$ownerID,"shop_id"=>$this->_shopID),"del_flag=0");

			$hasRef = 0; //是否关联
			if($ownerShopRe)
			{
				$hasRef = 1;
				if($ownerShopRe["c_discount_type"]==1)
				{
					$this->setViewParam("discount", "送积分");
				}
				else{
					$this->setViewParam("discount",$ownerShopRe["c_discount"]."折");
				}
				$this->setViewParam("ownerShopRe", $ownerShopRe);
			}
			else{
				$this->setViewParam("discount", "未关联");
			}
	
			$this->setViewParam("hasRef", $hasRef);
		}
		else{
			$ownerExist = -1;
		}

		$this->setViewParam("search_type", $s_type);
		$this->setViewParam("s_value", $s_value);
		$this->setViewParam("ownerExist", $ownerExist);
	}
	
	private function showError($msg,$type)
	{
		$url = "";
		if($type==1)
		{
			$url = "/merchant/integral/add";
		}
		else{
			$url = "/merchant/integral/reduce";
		}
		$this->sysMsg($msg,1,array(array("text"=>"返回","href"=>$url)));
	}
	
	public function addConfirm()
	{
		$this->_title= "增加积分-订单确认";
		
		$ownerID = 0;
		$discountType = 0;
		$balance = 0;
		$discount = 0;
		
		if(isset($_GET["isU"])) //确认页面本身提交检查密码
		{
			if(isset($_SESSION["addInfo"]))
			{
				$addInfo = $_SESSION["addInfo"];
				$ownerID = $addInfo["ownerID"];
				$discountType = $addInfo["discountType"];
				$balance = $addInfo["balance"];
				$discount = $addInfo["discount"];
			}
			else{
				$this->showError("操作超时",1);
			}
		}
		else{ //页面从增加积分提交到确认页面
			$ownerID = $this->getRequestData("txt_owner_id");
			$discountType = $this->getRequestData("r_discount");
			$balance = $this->getRequestData("txt_jifen");
			if(!CCommon::checkMoney($balance))
			{
				$this->showError("积分输入错误",1);
			}
			
			if($discountType!=1 && $discountType!=2)
			{
				$this->showError("参数错误",1);
			}
			
			if($discountType ==1)
			{
				$discount = $this->getRequestData("txt_jifen_add");
				if(!CCommon::checkMoney($discount))
				{
					$this->showError("赠送积分输入错误",1);
				}
			}
			else{
				$discount = $this->getRequestData("txt_discount");
				if(!CCommon::checkDiscount($discount))
				{
					$this->showError("折扣输入错误",1);
				}
			}
			
			$_SESSION["addInfo"] = array(
				"ownerID"=>$ownerID,
				"discountType"=>$discountType,
				"balance"=>$balance,
				"discount"=>$discount
			);
		}
		
		$ownerModel = new ownerModel();
		
		$where = array("owner_id"=>$ownerID);
		$ownerInfo = $ownerModel->selectByWhere("t_owner o,t_community c","o.*,c.community_name,c.province,c.city,c.area",$where, "o.community_id=c.community_id and o.del_flag=0");
		
		if(!$ownerInfo)
		{
			$this->showError("业主不存在或已删除",1);
		}
		
		if($ownerInfo["card_status"]==2)
		{
			$this->showError("业主卡片状态已冻结，无法操作",1);
		}
		
		if($ownerInfo["pay_status"]==2)
		{
			$this->showError("该业主已经关闭交易",1);
		}
		
		$ownerShopRe = $ownerModel->selectByWhere(
			"t_owner_shop",
			"os_id,owner_id,shop_id,balance,owner_card_status,c_discount_type,c_discount",
			array("owner_id"=>$ownerID,"shop_id"=>$this->_shopID),"del_flag=0");
		
		if($ownerShopRe)
		{
			if($ownerShopRe["owner_card_status"]==2)
			{
				$this->showError("该业主已经关闭增加或扣减积分",1);
			}
			if($ownerShopRe["balance"]>0 && $ownerShopRe["c_discount_type"]!=$discountType)
			{
				$this->showError("操作类型错误",1);
			}
		}
		

		if($_POST && isset($_GET["isU"]))
		{
			$pwd = $this->getRequestData("txt_pwd");
			$pwd = CCommon::pwdMd5($pwd);
				
			$shopModel = new shopModel();
			if(!$shopModel->checkPayPwd($this->_shopID,$pwd))
			{
				$this->setJsBlock("alert('帐号密码错误');");
			}
			else{
				unset($_SESSION["addInfo"]);
				$osModel = new ownerShopModel();
				$c_id = $osModel->addBalance($ownerInfo,$ownerShopRe,$ownerID,$this->_shopID,$balance,$discountType,$discount);
				if($c_id>0)
				{
					$this->redirectUrl("/merchant/integral/showInfo?type=1&cid=".$c_id);
				}
				else{
					$this->setJsBlock("alert('系统异常')");
				}
			}
		}
		
		$this->setViewParam("ownerInfo", $ownerInfo);
		$balance_t = 0;
		
		$hasRef = 0; //是否关联
		if($ownerShopRe)
		{
			$balance_t = $ownerShopRe["balance"];
			$hasRef = 1;
			if($ownerShopRe["c_discount_type"]==1)
			{
				$this->setViewParam("discount", "送积分");
			}
			else{
				$this->setViewParam("discount",$ownerShopRe["c_discount"]."折");
			}
			$this->setViewParam("ownerShopRe", $ownerShopRe);
		}
		else{
			$this->setViewParam("discount", "未关联");
		}
		$this->setViewParam("hasRef", $hasRef);
		
		
		$balance_t += $balance;
		if($discountType==1)
		{
			$balance_t += $discount;
		}

		$this->setViewParam("balance_t", $balance_t);
		
		$sInfo = array("balance"=>$balance,"discountType"=>$discountType,"discount"=>$discount);
		
		$this->render("integral/add_confirm",array("sInfo"=>$sInfo));
	}
	
	public function reduceConfirm()
	{
		$this->_title= "扣减积分-订单确认";
		
		$ownerID = 0;
		$balance = 0;
		$supplierID = -1;
		
		if(isset($_GET["isU"]))
		{
			if(!isset($_SESSION["sInfo"]))
			{
				$this->showError("操作超时",2);
			}
			$sInfo = $_SESSION["sInfo"];
			$ownerID = $sInfo["ownerID"];
			$balance = $sInfo["balance"];
			$supplierID = $sInfo["supplierID"];
		}
		else{
			$ownerID = $this->getRequestData("txt_owner_id");
			$balance = $this->getRequestData("txt_jifen");
			if(!CCommon::checkMoney($balance))
			{
				$this->showError("积分输入错误",2);
			}
			
			$supplierID = $this->getRequestData("sel_supplier");
			$_SESSION["sInfo"] = array(
				"ownerID"=>$ownerID,
				"balance"=>$balance,
				"supplierID"=>$supplierID
			);
		}
		
		$ownerModel = new ownerModel();
		$where = array("owner_id"=>$ownerID);
		$ownerInfo = $ownerModel->selectByWhere("t_owner o,t_community c","o.*,c.community_name,c.province,c.city,c.area",$where, "o.community_id=c.community_id and o.del_flag=0");
		
		if(!$ownerInfo)
		{
			$this->showError("业主不存在或已删除",2);
		}
		
		if($ownerInfo["card_status"]==2)
		{
			$this->showError("业主卡片状态已冻结，无法操作",2);
		}
		
		if($ownerInfo["pay_status"]==2)
		{
			$this->showError("该业主已经关闭交易",2);
		}
		
		$ownerShopRe = $ownerModel->selectByWhere("t_owner_shop",
				"os_id,owner_id,shop_id,balance,owner_card_status,c_discount_type,c_discount",
				array("owner_id"=>$ownerID,"shop_id"=>$this->_shopID),"del_flag=0");
		
		if($ownerShopRe)
		{
			if($ownerShopRe["owner_card_status"]==2)
			{
				$this->showError("该业主已经关闭增加或扣减积分",2);
			}
		}
		else{
			$this->showError("该业主未和商户关联",2);
		}
		
		$s_balance = $balance; //实际扣多少积分
		
		if($ownerShopRe["c_discount_type"] == 2) //如果当前优惠类型是打折
		{
			$this->setViewParam("discount", $ownerShopRe["c_discount"]."折");
			$s_balance = round($s_balance*$ownerShopRe["c_discount"],2);
		}
		else{
			$this->setViewParam("discount", "送积分");
		}
		
		$reduceAll = 0;
		if($s_balance>$ownerShopRe["balance"]) //实际扣除积分大于卡内余额
		{
			$reduceAll = 1;
		}
		
		if($_POST && isset($_GET["isU"]))
		{
			$pwd = $this->getRequestData("txt_pwd");
			$pwd = CCommon::pwdMd5($pwd);
			
			$isPass = true;
	
			if($ownerInfo["use_pay_pwd"]==1)
			{
				if(!$ownerModel->checkPayPwd($ownerID,$pwd))
				{
					$isPass = false;
					$this->setJsBlock("alert('帐号密码错误');");
				}
			}
			
			if($isPass)
			{
				if($reduceAll==1)
				{
					$s_balance = $ownerShopRe["balance"];
				}
				$osModel = new ownerShopModel();
				$c_id = $osModel->reduceBalance($ownerInfo,$ownerShopRe,$ownerID,$this->_shopID,$balance,$s_balance,$supplierID);
				if($c_id>0)
				{
					unset($_SESSION["sInfo"]);
					$this->redirectUrl("/merchant/integral/showInfo?type=2&cid=".$c_id);
				}
				else{
					$this->setJsBlock("alert('系统异常')");
				}
			}
		}
		$this->setViewParam("reduceAll", $reduceAll);
		$this->setViewParam("hasRef", 1);
		$this->setViewParam("balance", $balance);
		$this->setViewParam("s_balance", $s_balance); //实际扣减
		$this->setViewParam("balance_t", $ownerShopRe["balance"]-$s_balance); //剩余
		$this->setViewParam("ownerInfo", $ownerInfo);
		$this->setViewParam("ownerShopRe", $ownerShopRe);
		
		$this->render("integral/reduce_confirm");
	}
	
	public function showInfo()
	{
		$cid = $this->getRequestData("cid","get");
		$type = $this->getRequestData("type","get");
		
		if($type!=1 && $type!=2)
		{
			$this->sysMsg("参数错误",1);
		}
		
		if(!$cid)
		{
			$this->sysMsg("参数错误",1);
		}
		
		if($type==1)
		{
			$this->_title = "增加积分成功";
		}
		else
		{
			$this->_title = "扣减积分成功";
		}
		
		$icModel = new integralChangeModel();
		
		$icInfo = $icModel->selectById($cid,"shop_id=".$this->_shopID,"owner_id,cost_time,discount_type,cost_shop_before_money,cost_money,discount_num,cost_money_t,cost_shop_after_money");
		
		if(!$icInfo)
		{
			$this->sysMsg("交易不存在",1);
		}
		
		$ownerID = $icInfo["owner_id"];
		$where = array("owner_id"=>$ownerID);
		$ownerInfo = $icModel->selectByWhere("t_owner o,t_community c","o.supplementary_card, o.owner_name,c.community_name,c.province,c.city,c.area",$where, "o.community_id=c.community_id and o.del_flag=0");
		
		$this->render("integral/showInfo",array("type"=>$type,"cid"=>$cid, "icInfo"=>$icInfo,"ownerInfo"=>$ownerInfo));
	}
	
	public function p()
	{
		$cid = $this->getRequestData("cid","get");
		$type = $this->getRequestData("type","get");
		
		if($type!=1 && $type!=2)
		{
			$this->sysMsg("参数错误",1);
		}
		
		if(!$cid)
		{
			$this->sysMsg("参数错误",1);
		}
		
		if($type==1)
		{
			$this->_title = "增加积分成功";
		}
		else
		{
			$this->_title = "扣减积分成功";
		}
		
		$icModel = new integralChangeModel();
		
		$icInfo = $icModel->selectById($cid,"shop_id=".$this->_shopID,"owner_id,cost_time,discount_type,cost_shop_before_money,cost_money,discount_num,cost_money_t,cost_shop_after_money");
		
		if(!$icInfo)
		{
			$this->sysMsg("交易不存在",1);
		}
		
		$ownerID = $icInfo["owner_id"];
		$where = array("owner_id"=>$ownerID);
		$ownerInfo = $icModel->selectByWhere("t_owner o,t_community c","o.supplementary_card, o.owner_name,c.community_name,c.province,c.city,c.area",$where, "o.community_id=c.community_id and o.del_flag=0");
		
		$this->render("integral/print",array("type"=>$type, "icInfo"=>$icInfo,"ownerInfo"=>$ownerInfo),false);
	}
}